<?php
/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *  
 **/
 
 if (!defined('BASEPATH'))
	exit ('No direct script access allowed');

class Sources extends MY_Controller {
    function __construct() {

        parent::__construct();

        $this->checkLogin();
        $this->load->model('source_model', 'source', true);
        $this->load->helper('form');
        $this->template->set_template('admin');
        $this->buildMenu();
    }

	function Sources() {
		parent::__construct();
		$this->load->model('source_model', 'source', true);
		$this->load->helper('form');
		$this->main = '';
		$this->checkLogin();
		$this->checkPermission();
	}

	function index() { 
		$this->view();
	}
	
	function create(){
		$data = array();
		$data['id'] = 0;
		$data['vi_name'] = '';
		$data['en_name'] = '';
		$data['error_message'] = '';
		$data['legend_text'] = lang('create_source');
		$this->template->parse_view('content', 'admin/source/create_source', $data);
                $this->template->render();
	}	
	
	function edit($id_source) {
		$source = $this->source->getById($id_source);
		if(!$source){
			redirect('admin/sources');
			return;
		}
	
		$data = $source;
		
		$data['error_message'] = '';
		$data['legend_text'] = lang('edit_source');

                $this->template->parse_view('content', 'admin/source/create_source', $data);
                $this->template->render();
		//$this->main =  $this->load->view('admin/source/create_source', $data, true);
		//$this->execute();
	}
	
	function save() {
		$id = (int) $this->input->post('txtID');
		$source = new stdClass();
                $data = array();
		if($id > 0){
			$source = $this->source->getById($id);
			if(!$source){
				redirect('admin/sources');
				return;
			}
                        $data = $source;
		}
		else{
			$data['id'] = 0;
		}
		
		$data['vi_name'] = trim($this->input->post('txtViName'));
		$data['en_name'] = trim($this->input->post('txtEnName'));
				
		$error = false;
		$error_message = '';

		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[3]|max_length[50]');
		$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');
										
		if ($this->form_validation->run() == FALSE ){			
			$data['error_message'] = '<div style="color:red">' . validation_errors().'</div>';
			$data['legend_text'] = $id > 0 ?  lang('edit_source') : lang('create_source');

                        $this->template->parse_view('content', 'admin/source/create_source', $data);
                        $this->template->render();
		}
		else{				
			if($id > 0){					
				$result = $this->source->update($data);
			}
			else{				
				$this->source->vi_name = $data['vi_name'];
				$this->source->en_name = $data['en_name'];
				$id_source = $this->source->insert();				
			}
			
			redirect('admin/sources/');
		}
		
	}
	
	function view() {
		$NUMBER_RECORD_PER_PAGE = 15;
		$this->load->library('pagination');
		$page = (int)$this->uri->segment(4);
		$config['base_url'] = $this->base_url . 'admin/sources/view';
		$config['total_rows'] = $this->source->getAllCount();
		$config['per_page'] = $NUMBER_RECORD_PER_PAGE;
		$config['uri_segment'] = 4;
		$config['first_link'] = 'First';
		
		$this->pagination->initialize($config);
		
		$rows = $this->source->getAll($page, $NUMBER_RECORD_PER_PAGE);
		$data = array();
		$data['rows'] = $rows;
		
                $this->template->parse_view('content', 'admin/source/sources_list', $data);
                $this->template->render();
                //$this->main = $this->load->view('admin/source/sources_list', $data, true);
		//$this->execute();
	}

	function delete(){
		$id = intval($this->uri->segment(4));
		$ids = array();
 		if($id>0){
			$ids[] = (int)$id;
 			//$this->product->delete($id);
		}
 		else{
 			if(($this->input->post('submitDelcategory'))){
 				$ids = $this->input->post('categoryBox');
 			}
 		}
 		
		if(count($ids)){
	
 			$result = $this->source->delete($ids);
						
		}
		
 		redirect('admin/sources/');
	}
}
?>
